<template>
  <div>
    <BasicTable  @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 火情信息 </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'management:inFireInfo:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.enterpriseId }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'management:inFireInfo:edit'" type="text">
         <span style="font-size: 14px; color: #4169E1;">详情</span>
        </a-button>
        <a-button 
          @click="handleDelete(record)" 
          v-auth="'management:inFireInfo:edit'" 
          type="text" 
          style="color: red;"
          v-if="record.status == '9'"
        >
          {{ t('删除火情信息表') }}
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsManagementInFireInfoList">
  import { unref, watch, ref, computed, onMounted,nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { inFireInfoDelete, inFireInfoListData } from '/@/api/management/inFireInfo';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { officeTreeData } from '/@/api/sys/office';
  import { unitBuildingListData } from '/@/api/unitmanage/unitBuilding';
  import { unitRegionListData } from '/@/api/unitmanage/unitRegion';

  const props = defineProps({
    treeCode: String,
  });

  const { t } = useI18n('management.inFireInfo');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const route = router.currentRoute.value;

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('火情信息表管理'),
  };

  const EnterpriseParams = ref<Recordable>({
    name: ''
  });
  const buildingName = ref('')
  const selectParams = computed(() => {
    return {
      name: buildingName.value,
    };
  });
  const regionName = ref('');
  const regionParams = computed(() => {
    return {
      name: regionName.value,
    };
  });

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'enterpriseId',
        fieldLabel: 'enterpriseName',
        component: 'TreeSelect',
        componentProps: () => {
          return {
            placeholder: t('请输入主体名称'),
            api: unitEnterpriseDominantListData,
            params: EnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              EnterpriseParams.value.name = value;
              console.log(EnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'buildId',
        fieldLabel: 'buildName',
        component: 'TreeSelect',
        componentProps: {
          placeholder: t('请输入建筑名称'),
          api: unitBuildingListData,
          params: selectParams,
          resultField: 'list',
          fieldNames: { label: 'name', value: 'id' },
          allowClear: true,
          immediate: false,
          each: false,
          showSelect: true,
          optionFilterProp: 'label',
          showSearch: true,
          onSearch: (value) => {
            buildingName.value = value;
            // console.log('输入的值:', value);
            reload()
          },
        }
      },
      {
        label: t(''),
        field: 'regionId',
        fieldLabel: 'regionIname',
        component: 'Select',
        componentProps: {
          placeholder: t('请输入区域名称'),
          api: unitRegionListData,
          resultField: 'list',
          fieldNames: { label: 'name', value: 'id' },
          params: regionParams,
          allowClear: true,
          immediate: false,
          each: false,
          showSelect: true,
          optionFilterProp: 'label',
          showSearch: true,
          onSearch: (value) => {
            regionName.value = value;
            // console.log('输入的值:', value);
            reload()
          },
        }
      },
      {
        label: t(''),
        field: 'equipmentName',
        component: 'TreeSelect',
        componentProps: {
          placeholder: t('请输入设备名称'),
          api: inFireInfoListData,
          allowClear: true,
          resultField: 'list',
          fieldNames: { label: 'equipmentName', value: 'equipmentName' },
        },
      },
      {
        label: t(''),
        field: 'fireName',
        component: 'TreeSelect',
        componentProps: {
          placeholder: t('请输入火情名称'),
          api: inFireInfoListData,
          resultField: 'list',
          allowClear: true,
          fieldNames: { label: 'fireName', value: 'fireName' },
        },
      },
      {
        label: t(''),
        field: 'fireExecutionStatus',
        component: 'Select',
        componentProps: {
          placeholder: t('请选择火情执行状态'),
          allowClear: true,
          options: [
            { label: t('已结束'), value: '已结束' },
            { label: t('未处理'), value: '未处理' },
          ]
        }
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('企业名称'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('详细地址'),
      dataIndex: 'location',
      key: 'a.location',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('火情确认时间'),
      dataIndex: 'createTime',
      key: 'a.create_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('火情结束时间'),
      dataIndex: 'endTime',
      key: 'a.end_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('确认用户名称'),
      dataIndex: 'fireUserName',
      key: 'a.fire_user_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('火情执行状态'),
      dataIndex: 'fireExecutionStatus',
      key: 'a.fire_execution_status',
      sorter: true,
      width: 130,
      align: 'left',
      dictType:'fire_execution_status'
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 160,
      fixed: 'right',
      slot: 'action'
    }
  ];

  const [registerTable, { reload, getForm }] = useTable({
    api: async (params) => {
      const route = router.currentRoute.value;
      if (route.query.tid) {
        params.tid = route.query.tid;
      }
      return inFireInfoListData(params);
    },
    beforeFetch: (params, data) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();

  watch(
    () => props.treeCode,
    async () => {
      await getForm().setFieldsValue({
        'Fire_Hazard_Process': props.treeCode,
      });
      reload();
    },
  );

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await inFireInfoDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  onMounted(() => {
    const { tid, openDrawer: shouldOpenDrawer } = route.query;
    if (shouldOpenDrawer === 'true' && tid) {
      handleForm({ tid });
    }
    reload();
  });
</script>    